﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
public partial class Admin_EditContact : System.Web.UI.Page
{
    ContactBll cBll = new ContactBll();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["state"] != null)
            {
                string state = Request.QueryString["state"].ToString();
                switch (state.Trim())
                {
                    case "add":
                        add();
                        break;
                    case "edit":
                        EditContact(Convert.ToInt32(Request.QueryString["id"]));
                        break;
                    default:
                        break;
                }
            }

        }
    }

    private void add()
    {
        #region 验证信息
        if (RegularExpressions.ReEmail(Request.Form["Email"].ToString()) == false)
        {
            Response.Write("电子邮件格式错误");
            Response.End();
        }
        else if (Request.Form["FullName"].ToString() == "")
        {
            Response.Write("名称不能为空");
            Response.End();
        }
        #endregion
        Contact ct = new Contact();
        ct.Name = Request.Form["FullName"].ToString();
        ct.Post = Request.Form["Duty"].ToString();
        ct.qq = Request.Form["qq"].ToString();
        ct.Email = Request.Form["Email"].ToString();
        ct.Tel = Request.Form["Mobile"].ToString();
        ct.contactId = Convert.ToInt32(Request.Form["Hiddenid"]);
        ct.Sex = Convert.ToBoolean(Convert.ToInt32(Request.Form["sex"]));
        ct.contactDate = DateTime.Now;
        if (cBll.Update(ct))
        {
            Response.Write("1");
            Response.End();
        }
    }
    public void EditContact(int id)
    {
        Contact ct = cBll.GetModel(id);
        this.FullName.Value = ct.Name;
        this.Duty.Value = ct.Post;
        this.qq.Value = ct.qq;
        this.Email.Value = ct.Email;
        this.Mobile.Value = ct.Tel;
        if (ct.Sex)
        {
            this.Sex_1.Checked = true;
        }
        else
        {
            this.Sex_2.Checked = true;
        }
        this.Hiddenid.Value = id.ToString();
    }
}
